"""
Case Type   : HTAP行列融合特性
Case Name   : 设置max_imcs_cache值大于max_process_memory值
Create At   : 2025/1/23
Owner       : ningyali
Description :
    1.获取max_process_memory值
    2.设置max_imcs_cache值大于max_process_memory值
    3.清理数据环境
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class HTAP(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.Constant = Constant()
        self.common = Common()

    def test_trans(self):
        text = '----step1:获取max_process_memory值;expect:执行成功----'
        self.log.info(text)
        sql = f'show max_process_memory;'
        sql_res = self.pri_sh.execut_db_sql(sql)
        rows, _ = self.common.parse_sql_query_res(sql_res)
        self.log.info(f'rows:{rows}')
        value = int(rows[0][0].strip().strip('GB'))
        self.log.info(f'value:{value}')
        self.assertGreater(value, 1, f'执行失败:{text}')

        text = '----step2:设置max_imcs_cache值大于max_process_memory值;' \
               'expect:执行成功----'
        self.log.info(text)
        guc_res = self.pri_sh.execute_gsguc(
            'set', self.Constant.GSGUC_SUCCESS_MSG,
            f'max_imcs_cache={value + 1}GB')
        self.assertEqual(True, guc_res, f'参数配置失败:{text}')

    def tearDown(self):
        text = '----step3:清理环境;expect:执行成功----'
        self.log.info(text)
        guc_res = self.pri_sh.execute_gsguc(
            'set', self.Constant.GSGUC_SUCCESS_MSG,
            f'max_imcs_cache')
        is_restarted = self.pri_sh.restart_db_cluster()

        self.assertTrue(guc_res, f'参数配置失败:{text}')
        self.assertTrue(is_restarted, f'重启数据库失败:{text}')

        self.log.info(f'-----{os.path.basename(__file__)} end-----')
